/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.web.client;

import com.cleangwt.json.client.ObjectSerializer;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestBuilder.Method;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;

/**
 * This class provides methods for sending requests.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class Requests {

  public static final String DEFAULT_URI_SUFFIX = ".do";

  /**
   * Builds uri with {@link #DEFAULT_URI_SUFFIX} {@value #DEFAULT_URI_SUFFIX}.
   * 
   * @param domain the domain
   * @param target the target
   * @return the uri
   * @see #buildUri(String, String, String)
   */
  public static String buildUrl(String domain, String target) {
    return buildUri(domain, target, DEFAULT_URI_SUFFIX);
  }

  /**
   * Builds a uri with the given domain, target and uri suffix.
   * 
   * <pre>
	 * Assume : 
	 * 1.the given domain is 'user',
	 * 2.the target is 'update',
	 * 3.the uri suffix is '.do'
	 * 
	 * The output would be : 'user/update.do'
	 * </pre>
   * 
   * @param domain the domain name
   * @param target the target name
   * @param suffix the suffix should be appended to the end of uri
   * @return the uri
   */
  public static String buildUri(String domain, String target, String suffix) {
    return new StringBuilder().append(domain).append("/").append(target).append(suffix).toString();
  }

  /**
   * Sends a 'GET' request to the specified uri.
   * 
   * @param uri the uri
   * @param callback the callback to process response
   * @return the request can be canceled
   */
  public static Request get(String uri, RequestCallback callback) {
    return send(new RequestBuilder(RequestBuilder.GET, uri), null, callback);
  }

  /**
   * Sends a 'POST' request to the specified uri with request data.
   * 
   * @param uri the uri
   * @param requestData the request data
   * @param callback the callback to process response
   * @return the request can be canceled
   */
  public static Request post(String uri, String requestData, RequestCallback callback) {
    return send(new RequestBuilder(RequestBuilder.POST, uri), requestData, callback);
  }

  /**
   * Sends a 'POST' json request to the given uri with http method, json and callback to process
   * response.
   * 
   * @param uri the uri
   * @param method the http method
   * @param json the json
   * @param callback the callback to process response
   * @return the request can be canceled
   */
  public static Request postJson(String uri, String json, RequestCallback callback) {
    return sendJson(uri, RequestBuilder.POST, json, callback);
  }

  /**
   * Sends a 'POST' json request to the specified uri with the object to serialize to json and the
   * callback to process response.
   * 
   * @param uri the uri
   * @param object the object to serialize to json
   * @param callback the callback to process response
   */
  public static Request postObject(String uri, Object object, RequestCallback callback) {
    return sendObject(uri, RequestBuilder.POST, object, callback);
  }

  /**
   * Sends request to the given uri with request data and callback to process response.
   * 
   * @param uri the uri
   * @param requestData the request data
   * @param callback callback to process response
   * @return the request can be canceled
   */
  public static Request send(String uri, Method method, String requestData, RequestCallback callback) {
    return send(new RequestBuilder(method, uri), requestData, callback);
  }

  /**
   * Sends a json request to the given uri with http method, json and callback to process response.
   * 
   * @param uri the uri
   * @param method the http method
   * @param json the json
   * @param callback the callback to process response
   * @return the request can be canceled
   */
  public static Request sendJson(String uri, Method method, String json, RequestCallback callback) {
    RequestBuilder rb = new RequestBuilder(method, uri);
    rb.setHeader("Accept", "application/json");
    rb.setHeader("Content-Type", "application/json");
    return send(rb, json, callback);
  }

  /**
   * 
   * Sends a json request to the given uri with the http method, the object to serialize to json and
   * the callback to process response.
   * 
   * @param uri the uri
   * @param method the http method
   * @param object the object to serialize to json
   * @param callback the callback to process response
   * @return the request can be canceled
   */
  public static Request sendObject(String uri, Method method, Object object,
      RequestCallback callback) {
    return sendJson(uri, method, ObjectSerializer.get().serialize(object), callback);
  }

  /**
   * Sends request for the given request builder with the request data, the callback to process
   * response and the defalut exception handler.
   * 
   * @param builder the request builder
   * @param requestData the request data
   * @param callback callback to process response
   * @return the request can be canceled
   */
  private static Request send(RequestBuilder builder, String requestData, RequestCallback callback) {
    try {
      return builder.sendRequest(requestData, callback);
    } catch (RequestException e) {
      callback.onError(null, e);
      return null;
    }
  }

}
